package net.qhzw.vip.qiyu.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.entity.SysMenu;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.SysMenuService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/***
 * 菜单管理
 */
@RequestMapping("sys/menu")
@RestController
@Api(tags = "菜单管理")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping
    @ApiOperation(value = "查询全部菜单")
    //@PreAuthorize("hasAuthority('sys_role_query')")
    public JsonResult<List<SysMenu>> findList() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> sysMenus = sysMenuService.findList(userId);
        return JsonResult.ok(sysMenus);
    }

    @GetMapping("{id}")
    public JsonResult<SysMenu> getSysMenu(@PathVariable Long id) {
        SysMenu sysMenu = sysMenuService.getById(id);
        return JsonResult.ok(sysMenu);
    }

    @ApiOperation(value = "添加菜单")
    @PostMapping
    public JsonResult addMenu(@RequestBody SysMenu sysMenu) {
        boolean save = sysMenuService.addMenu(sysMenu);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "修改菜单")
    @PutMapping
    public JsonResult updateMenu(@RequestBody SysMenu sysMenu) {
        boolean save = sysMenuService.updateMenu(sysMenu);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "根据id删除菜单")
    @DeleteMapping("{id}")
    public JsonResult delete(@PathVariable Long id) {
        int count = sysMenuService.findChildCount(id); //
        if (count > 0) {
            return JsonResult.fail("请先删除子节点");
        }
        //逻辑删除
        boolean remove = sysMenuService.tombstone(id);
        if (remove) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


}
